﻿using System.Text.RegularExpressions;

namespace NHDay_Linq.ProviderExtensions
{
	public static class IsLikeLinqExtension
	{
		/// <summary>
		/// An extension method to mimic the usual SQL Like operator in Linq (linq does not have an islike function)
		/// </summary>
		/// <param name="source">The source.</param>
		/// <param name="pattern">The pattern.</param>
		/// <returns>
		/// 	<c>true</c> if the specified source is like; otherwise, <c>false</c>.
		/// </returns>
		public static bool IsLike(this string source, string pattern)
		{
			pattern = Regex.Escape(pattern);
			pattern = pattern.Replace("%", ".*?").Replace("_", ".");
			pattern = pattern.Replace(@"\[", "[").Replace(@"\]", "]").Replace(@"\^", "^");

			return Regex.IsMatch(source, pattern);
		}
	}
}